package com.yksj.healthtalk.ui.doctor;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.ui.R;

/**
 * 医生管科室列表
 * 
 * @author Administrator
 * 
 */
public class DoctorDepartmentsListActivity extends BaseFragmentActivity implements OnItemClickListener {

	private ListView mListView;
	private ArrayList<HashMap<String, String>> list;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.doctor_department_list);
		initWidget();
		initData();
	}
	private void initData() {
		HttpRestClient.doHttpDcotorDepartments(
				null,
				new JsonsfHttpResponseHandler(this) {
					@Override
					public void onSuccess(int statusCode,JSONArray response) {
						list = new ArrayList<HashMap<String, String>>();
						HashMap<String, String> map = new HashMap<String, String>();
						for (int i = 0; i < response.size(); i++) {
							try {
								map = new HashMap<String, String>();
								JSONObject object = response.getJSONObject(i);
								map.put("OFFICE_CODE", object.getString("OFFICE_CODE"));
								map.put("OFFICE_NAME", object.getString("OFFICE_NAME"));
								map.put("ANDROID_ICON", object.getString("ANDROID_ICON"));
								list.add(map);
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
						DepartmentAdapter adapter = new DepartmentAdapter(list);
						mListView.setAdapter(adapter);
						super.onSuccess(statusCode, response);
					}
				}
				);
	}

	private void initWidget() {
		// TODO Auto-generated method stub
		initTitle();
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setBackgroundResource(R.drawable.icon_search_press);
		titleRightBtn2.setOnClickListener(new OnClickListener() {// 搜索
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(DoctorDepartmentsListActivity.this, DoctorExactSearchActivity.class);
				startActivity(intent);
			}
		});
		
		titleLeftBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onBackPressed();
			}
		});
		titleTextV.setText("按科室查找");
		mListView = (ListView) findViewById(R.id.list);
		mListView.setOnItemClickListener(this);
	}

	class DepartmentAdapter extends BaseAdapter {
		private ArrayList<HashMap<String, String>> list;
		private ViewHolder holder;
		private String str;

		public DepartmentAdapter(ArrayList<HashMap<String, String>> list){
			this.list = list;
		}
		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(DoctorDepartmentsListActivity.this).inflate(R.layout.department_item, null);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.iv = (ImageView)convertView.findViewById(R.id.iv);
				convertView.setTag(holder);
			}else {
				holder = (ViewHolder) convertView.getTag();
			}
			str = list.get(position).get("ANDROID_ICON");
			holder.name.setText(list.get(position).get("OFFICE_NAME"));
			holder.iv.setVisibility(View.VISIBLE);
			if(str.contains("adr_bx")){
				holder.iv.setImageResource(R.drawable.adr_bx);			
				}else if(str.contains("adr_crk")){
				holder.iv.setImageResource(R.drawable.adr_crk);			
				}else if(str.contains("adr_ebhk")){
					holder.iv.setImageResource(R.drawable.adr_ebhk);			
				}else if(str.contains("adr_ek")){
					holder.iv.setImageResource(R.drawable.adr_ek);			
				}else if(str.contains("adr_fck")){
					holder.iv.setImageResource(R.drawable.adr_fck);			
				}else if(str.contains("adr_fzjck")){
					holder.iv.setImageResource(R.drawable.adr_fzjck);			
				}else if(str.contains("adr_fzzlk")){
					holder.iv.setImageResource(R.drawable.adr_fzzlk);			
				}else if(str.contains("adr_kqk")){
					holder.iv.setImageResource(R.drawable.adr_kqk);			
				}else if(str.contains("adr_mzk")){
					holder.iv.setImageResource(R.drawable.adr_mzk);			
				}else if(str.contains("adr_nk")){
					holder.iv.setImageResource(R.drawable.adr_nk);			
				}else if(str.contains("adr_pfxbk")){
					holder.iv.setImageResource(R.drawable.adr_pfx);			
				}else if(str.contains("adr_qtjbk")){
					holder.iv.setImageResource(R.drawable.adr_qtjbk);			
				}else if(str.contains("adr_qtks")){
					holder.iv.setImageResource(R.drawable.adr_qtks);			
				}else if(str.contains("adr_jsxlk")){
					holder.iv.setImageResource(R.drawable.adr_sjxlk);			
				}else if(str.contains("adr_wk")){
					holder.iv.setImageResource(R.drawable.adr_wk);			
				}else if(str.contains("adr_yk")){
					holder.iv.setImageResource(R.drawable.adr_yk);			
				}else if(str.contains("adr_zlk")){
					holder.iv.setImageResource(R.drawable.adr_zlk);			
				}else if(str.contains("adr_zyk")){
					holder.iv.setImageResource(R.drawable.adr_zyk);			
				}else if(str.contains("adr_gk")){
					holder.iv.setImageResource(R.drawable.adr_gk);			
				}else{
					holder.iv.setVisibility(View.INVISIBLE);
				}
			return convertView;
		}

	}
	
	class ViewHolder {
		TextView name;
		ImageView iv;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = new Intent(this,DoctorMainUI.class);
		HashMap<String, String> map = list.get(position);
		if(!"全部".equals(map.get("OFFICE_NAME"))){
			intent.putExtra("name",map.get("OFFICE_NAME"));
		}
		intent.putExtra("code", map.get("OFFICE_CODE"));
		startActivity(intent);
	}

}
